<template>
  <BaseFormField
    v-slot="{ fieldId }"
    :resource-name="resourceName"
    :field="field"
    :value="modelValue"
    :is-floating="isFloating"
  >
    <FormFieldGroup
      :field="field"
      :label="field.label"
      :field-id="fieldId"
      :validation-errors="validationErrors"
    >
      <div>
        <IColorSwatch
          :model-value="modelValue"
          :swatches="$scriptConfig('favourite_colors')"
          :name="field.attribute"
          :allow-remove="!field.isRequired"
          @update:model-value="updateModelValue"
        />
      </div>
    </FormFieldGroup>
  </BaseFormField>
</template>

<script setup>
import isNil from 'lodash/isNil'

import FormFieldGroup from '../FormFieldGroup.vue'

const props = defineProps({
  field: { type: Object, required: true },
  modelValue: {},
  resourceName: String,
  resourceId: [String, Number],
  validationErrors: Object,
  isFloating: Boolean,
})

const emit = defineEmits(['update:modelValue', 'set:initialValue'])

function updateModelValue(value) {
  emit('update:modelValue', value)
}

function setInitialValue() {
  emit('set:initialValue', !isNil(props.field.value) ? props.field.value : '')
}

setInitialValue()
</script>
